<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Law;
use App\Model\LawDetail;
use App\Model\LawCategory;
use App\Model\Learn;
use Auth;

class LawController extends Controller
{
    /*
    *获取法律法规列表
    *@param type 分类id category 执行方式
    *@return array 法律法规列表和是否有权限下载
    */
    public function list(Request $request)
    {
        $categoryId = $request->input('category',0);
        $checkCategoryId = $request->input('type', 0);
        $title = $request->input('title','');
        if(empty($categoryId)) {
            return response()->json(array('message'=>'参数错误', 'code'=>'400'), 400);
        }

        $where = array();
        $where[] = array('categoryId', $categoryId);
        $where[] = array('checkCategoryId', $checkCategoryId);
        if(!empty($title)) {
            $where[] = array('title', 'like', '%'.$title.'%');
        }
        $fields = array('id', 'title', 'publishTime', 'downUrl');
        $list = Law::where($where)->orderBy('updateTime', 'DESC')->select($fields)->get();
        $list = json_decode(json_encode($list), true);
        $returnList = $list;
        foreach($returnList as $k=>$v) {
            $returnList[$k]['detailUrl'] = route('api_law_detail',array('id'=>$v['id']));
        }

        /*根据用户判断是否有下载权限*/
        $user = Auth::user();
        if($user['type']!==1) {
            foreach($returnList as $k=>$v) {
                unset($returnList[$k]['downUrl']);
            }
        }

        return response()->json(array('data'=>$returnList,'message'=>'返回数据成功','code'=>200),200);
    }
    /*
    *法律法规详情
    *@param id 法律法规id
    */
    public function detail(Request $request)
    {
        $id = $request->input('id');
        
        $law = Law::where('id', $id)->first();

        $law_detail = LawDetail::where('id', $id)->first();
        $law_detail['content'] = html_entity_decode($law_detail['content']);

        /*浏览量+1*/
        Law::where('id', $id)->update(array('hits'=>$law['hits']+1));

        return view('api.law.detail', [
            'law' => $law,
            'law_detail' => $law_detail
        ]);
    }

    /*
    *法律法规分类依据
    */
    public function basis(Request $request)
    {
        $id = 13;
        $learn = Learn::find($id);
        return view('api.law.basis',[
            'learn' => $learn
        ]);
    }


}
